package com.scau.action.business;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ModelDriven;
import com.scau.action.BaseAction;
import com.scau.exception.BusinessException;
import com.scau.model.business.Supplier;
import com.scau.service.impl.business.SupplierService;
import com.scau.util.PageController;

@Component
@Scope("prototype")
public class SupplierAction extends BaseAction implements  Serializable ,ModelDriven<Supplier>{
	private final static Log logger = LogFactory
			.getLog(MaterialTypeAction.class);
	private PageController pager = null;
	private Supplier supplier = null;
	private SupplierService supplierService = null;
	private List<Supplier> supplierList = null;

	/**
	 * 列表显示
	 * @return
	 */
	public String list() {
		int totalRows = supplierService.getRecordCount(new Supplier());
		String URL =getListURL();
		this.pager.setURL(URL);
		this.pager.setTotalRowsAmount(totalRows);
		supplierList = supplierService
				.list(new Supplier(), this.pager.getPageStartRow(),
						pager.getPageSize(), null,null);
		pager.setData(supplierList);
		return "list";
	}

	/**
	 * 修改
	 * @return
	 */
	public String get() {
		supplier = supplierService.get(supplier);
		return "edit";
	}
	
	/**
	 * 保存
	 * @return
	 */
	public String save() {
		try {
			supplierService.save(supplier);
			return list();
		} catch (BusinessException e) {
			// 保存原来表单已输入的内容
			request.setAttribute("supplier", supplier);
			request.setAttribute("message", e.getMessage());
			return "edit";
		}
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String del() {
		String[] ids = request.getParameterValues("id");
		for (String supplierId : ids) {
			Supplier sp = new Supplier();
			if (null != supplierId && !"0".equals(supplierId)) {
				supplierService.delete(sp,Long.parseLong(supplierId));
			}
		}
		return list();
	}
		

	public PageController getPager() {
		return pager;
	}

	@Resource
	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	

	public SupplierService getSupplierService() {
		return supplierService;
	}

	@Resource
	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}

	public List<Supplier> getSupplierList() {
		return supplierList;
	}

	public void setSupplierList(List<Supplier> supplierList) {
		this.supplierList = supplierList;
	}

	@Override
	public Supplier getModel() {
		// TODO Auto-generated method stub
		return supplier;
	}

}
